package com.chengcang.api.modules.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chengcang.api.modules.busi.dao.BusiDemandAddressSnapshotDao;
import com.chengcang.api.modules.busi.dto.BusiAddressDTO;
import com.chengcang.api.modules.busi.dto.BusiDemandAddressSnapshotDTO;
import com.chengcang.api.modules.busi.entity.BusiDemandAddressSnapshotEntity;
import com.chengcang.api.modules.busi.service.BusiAddressService;
import com.chengcang.api.modules.busi.service.BusiDemandAddressSnapshotService;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 地区表
 *
 * @author Zhanli xy321231@163.com
 * @since 1.0.0 2023-02-20
 */
@Service
public class BusiDemandAddressSnapshotServiceImpl extends CrudServiceImpl<BusiDemandAddressSnapshotDao, BusiDemandAddressSnapshotEntity, BusiDemandAddressSnapshotDTO> implements BusiDemandAddressSnapshotService {


    @Autowired
    private BusiAddressService busiAddressService;

    @Override
    public QueryWrapper<BusiDemandAddressSnapshotEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<BusiDemandAddressSnapshotEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public BusiDemandAddressSnapshotEntity saveDemandSnapshot(Long demandId, Long addressId) {
        BusiAddressDTO dto = busiAddressService.get(addressId);
        BusiDemandAddressSnapshotEntity entity = ConvertUtils.sourceToTarget(dto, BusiDemandAddressSnapshotEntity.class);
        entity.setId(null);
        entity.setAddressId(addressId);
        entity.setDemandId(demandId);
        this.insert(entity);
        return entity;
    }

    @Override
    public List<BusiDemandAddressSnapshotDTO> selectBatchIds(List<Long> addressIds) {
        List<BusiDemandAddressSnapshotEntity> list = baseDao.selectBatchIds(addressIds);
        return ConvertUtils.sourceToTarget(list, BusiDemandAddressSnapshotDTO.class);
    }


}